#!/usr/bin/env ruby
#
#  Created by sjavey on 2007-07-10.
#  Copyright (c) 2007. All rights reserved.

#
# To run this program you must add /full-path.../note19/trunk to your RUBYLIB
# environment variable.
#

require 'n19gpx/document'

class ElevationRecord
  
  attr_reader :elevation_time
  attr_reader :gpx
  
  def initialize(gpxfile)
    @gpx = N19GPX::Document.new(gpxfile)
    @index = -1
    @counter = -1
    @elevation_time = ""
  end
    
  def addPoint(point) 
    @elevation_time << "{ x: #{point.pst_hour_minute} , y: #{point.ele} , lat:#{point.lat}, lon:#{point.lon}},\n"
  end
  
  def trackHtml(track)
    track.points.each { |point|
      addPoint(point)
    }
  end

  def html
    @htmlHeader = <<EOS
    #{elevationRecord()}
EOS
  end
  
  def tracksHtml
    @gpx.tracks.each {|track|
     trackHtml(track)
    }
  end
  
  def timeLabel
    min = @gpx.to_hour_min(@gpx.start_time)
    max = @gpx.to_hour_min(@gpx.end_time)
    i = min
    s = ""
    while (i < max) 
      s << sprintf("%.6f", i) << ","
      i = i + 0.5
    end
    return s << sprintf("%.6f",max)
  end
  
  def elevationRecord
    tracksHtml
    str = <<EOS
      public var elevationData : ArrayCollection = new ArrayCollection ([
        #{@elevation_time[0..@elevation_time.length-3]}
      ]);
      public var elevationMin : Number = #{@gpx.min_ele};
      public var elevationMax : Number = #{@gpx.max_ele};
      public var timeMin : Number = #{@gpx.to_hour_min(@gpx.start_time)};
      public var timeMax : Number = #{@gpx.to_hour_min(@gpx.end_time)};
      
EOS
  end
  
end

# test the googlemap generation

track = "14-oct-2007"
gmap = ElevationRecord.new(track + ".gpx")
file = File.new(track + "-elevation.txt", "w")
file.puts gmap.html()
file.close
